require("GameObjects.Units.Heavy_Horse")
require("GameObjects.Units.Archer")
require("GameObjects.Units.SwordFighter")
require("Helpers.MovementHelper")
require("Helpers.AttackHelper")

InputHelper = {}
InputHelper.__index = InputHelper
 
function InputHelper.new()
    local instance = {
    }
    
    setmetatable(instance, InputHelper)
    return instance
end

function InputHelper:setupInput()
  if(MOAIInputMgr.device.keyboard) then
    MOAIInputMgr.device.keyboard:setCallback(
        function(key,down)
          self:handleKeyBoard(key, down)
          
          if currentScreen ~= typesOfScreens["mainmenu"] then            
            if down == true and key == 27 then
              showMainMenu()
              audio:stopLevelBackgroundAudio()
            end
          end
                  
          if down == true and isVictoryScreen == true then
              if key ~= 116 then
                MainLayer:removeProp(victoryProp)
                MainLayer:removeProp(winMessage)
                showMainMenu()
                audio:stopLevelBackgroundAudio()
            end
          end
        end
    )
  end
  
  if MOAIInputMgr.device.pointer then
    MOAIInputMgr.device.mouseLeft:setCallback(
        function(isMouseDown)
            if(isMouseDown) then
                self:handleSingleClick(MOAIInputMgr.device.pointer:getLoc())
            end
        end
    )  
    MOAIInputMgr.device.mouseRight:setCallback(
        function(isMouseDown)
            if(isMouseDown) then
              if doneButtonShowed == false then
                doneButtonProp:moveLoc(0, 50, 1)
                if isProductionOverlayShowed == true then
                  propOverlay:moveLoc(0, -80, 1)
                end
                doneButtonShowed = true
              end
            end
        end
    )  
  else
      MOAIInputMgr.device.touch:setCallback (
          function( eventType, idx, x, y, tapCount)
              if (tapCount > 1) then
                
                if down == true and isVictoryScreen == true then
                    MainLayer:removeProp(victoryProp)
                    MainLayer:removeProp(winMessage)
                    showMainMenu()
                    audio:stopLevelBackgroundAudio()
                end
                
                if doneButtonShowed == false then
                  doneButtonProp:moveLoc(0, 50, 1)
                  if isProductionOverlayShowed == true then
                    propOverlay:moveLoc(0, -80, 1)
                end
                doneButtonShowed = true
              end
              elseif eventType == MOAITouchSensor.TOUCH_DOWN then
                  self:handleSingleClick(x, y)
                end
          end
      )
  end
end

function InputHelper:handleKeyBoard(key,down)
  if currentScreen == typesOfScreens["game"] and down == true then	
     if down == true and key == 116 then
       endTurn()
     end
    
    local amountMoving = 64
    
    -- links  
    if key == 100 and camMapX < ((xMap * 64) - baseScreenWidth) then
      moveCamera(amountMoving, 0, amountMoving, "left")
      camMapX = camMapX + 64
      tempX1 = tempX1 + 1
    end
    
    -- rechts
    if key == 97 and camMapX > 0 then
      moveCamera(-amountMoving, 0, amountMoving, "right")
      camMapX = camMapX - 64
      tempX2 = tempX2 - 1
    end
 
    -- onder
    if key == 119 and camMapY - sizeMenuBalk > 0 then
      moveCamera(0, amountMoving, amountMoving, "down")
      camMapY = camMapY - 64
    end
    
    -- boven
    if key == 115 and camMapY - sizeMenuBalk < ((yMap * 64) - baseScreenHeight) then
      moveCamera(0, -amountMoving, amountMoving, "up")
      camMapY = camMapY + 64
    end
  end
end

function InputHelper:handleSingleClick(x, y)
  if currentScreen == typesOfScreens["mainmenu"] then
    screenActionHelper:mainmenuAction(x,y)
  elseif currentScreen == typesOfScreens["chooselevel"] then
    screenActionHelper:chooselevelAction(x,y)
  elseif currentScreen == typesOfScreens["game"] then
    screenActionHelper:gameAction(x,y)
  elseif currentScreen == typesOfScreens["guide"] then
    screenActionHelper:guideAction(x,y)
  end
end

return InputHelper